﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

using JupiterLibrary;

namespace KinnectInput.KinectIcons
{
    public enum KinectButtonState
    {
        Obsucred,
        InActive,
        Active,
        Selected
    }
    public class KinectButton
    {
        Texture2D tex;
        Sprite spriteTex;
        Sprite selectedMask;
        public Vector2 Position;
        //Todo hacer que sea resizable el boton quan sea
        Rectangle _buttonArea;
        public Rectangle ButtonArea 
        {
            get 
            {
                return _buttonArea;
            }

            set
            {
                _buttonArea = value;
                if (spriteTex != null)
                {
                    spriteTex.SetRectangle(value);
                }
            }
        }
        KinectButtonState buttonState;

        public KinectButtonState ButtonState
        {
            get
            {
                return buttonState;
            }

            set
            {
                LastButtonState = buttonState;
                buttonState = value;
            }
        }
        public KinectButtonState LastButtonState;
        public Ring SelectionButtonRing;

        float scale;
        float timeToScale;
        public KinectButton(Texture2D tex,Vector2 Position)
        {
            this.tex=tex;
            this.Position = Position;
            this.ButtonArea = new Rectangle((int)Position.X, (int)Position.Y, tex.Bounds.Width, tex.Bounds.Height);
            SelectionButtonRing = new Ring(0, 0, 75, 25);
            SelectionButtonRing.Color = new Color(0, 0.9f, 0.9f, 0.5f);
            ButtonState=KinectButtonState.InActive;
            spriteTex = new Sprite();
            spriteTex.Texture = tex;
            spriteTex.UseMatrix = true;
            spriteTex.SetRectangle(ButtonArea);
            selectedMask = new Sprite();
            selectedMask.SetColor(new Color(255, 255, 255, 150));
            selectedMask.SetRectangle(ButtonArea);
        }
        public void Update()
        {
            if (Position.X != ButtonArea.X || Position.Y != ButtonArea.Y)
            {
                ButtonArea = new Rectangle((int)Position.X, (int)Position.Y, tex.Bounds.Width, tex.Bounds.Height);
                spriteTex.SetRectangle(ButtonArea);
            }

            if (this.ButtonState == KinectButtonState.Active && SelectionButtonRing.EndAngle < 361)
            {
                SelectionButtonRing.EndAngle += Jupiter.At1Second(361);
                SelectionButtonRing.Update();
            }
            else if (this.ButtonState == KinectButtonState.Active && SelectionButtonRing.EndAngle > 360)
            {
                this.ButtonState = KinectButtonState.Selected;
            }
            else if (this.ButtonState == KinectButtonState.Selected && LastButtonState==KinectButtonState.Active)
            {
            //    scale = 1 + 0.5f*(float)Math.Abs(Math.Sin(timeToScale));
            //    timeToScale += Jupiter.At1Second(2);
            //    spriteTex.Matrix = Matrix.CreateScale(scale, scale, 1) * Matrix.CreateTranslation(new Vector3(this.ButtonArea.Center.X, this.ButtonArea.Center.Y, 0));
                spriteTex.Matrix = Matrix.CreateScale(1.2f, 1.2f, 1) * Matrix.CreateTranslation(new Vector3(this.ButtonArea.Center.X, this.ButtonArea.Center.Y, 0));
            }
            else
            {
                spriteTex.Matrix = Matrix.CreateScale(1, 1, 1) * Matrix.CreateTranslation(new Vector3(this.ButtonArea.Center.X, this.ButtonArea.Center.Y, 0));
            }

            if (ButtonState == KinectButtonState.InActive)
            {
                spriteTex.SetAlpha(0.5f);
            }
            else
            {
                spriteTex.SetAlpha(1f);
            }

            spriteTex.Update();
        }
        public void CheckActivity(Vector2 Hand)
        {
            if (ButtonArea.Contains(new Point((int)Hand.X, (int)Hand.Y)))
            {
                this.ButtonState = KinectButtonState.Active;
                SelectionButtonRing.Position = new Vector3(Hand.X,Hand.Y,0);
            }
            else if(ButtonState != KinectButtonState.Selected)
            {
                this.ButtonState = KinectButtonState.InActive;
                SelectionButtonRing.EndAngle = 0;
                SelectionButtonRing.Update();
            }
            
        }
        public void CheckActivity(Point p)
        {
            CheckActivity(new Vector2(p.X, p.Y));
        }
        public void Draw()
        {

           
            spriteTex.Draw();
            
            
        }
        
    }
}
